use edp_derive::edp_service;
use edp_protocol::TaskStatus;
use serde_json::{json, Value};
#[edp_service(
    name = "rust-calculator",
    desc = "Rust Calculator",
    category = "test",
    tasks = [
        "list_add_1: List<Int>->List<Int>",
        "#instant int_mul_2: Int->Int"
    ]
)]
struct Calculator;
impl Calculator {
    pub fn list_add_1(&self, inputs: Value) -> Result<Value, String> {
            let arr = inputs.as_array().ok_or("invalid input")?;
            let mut res = vec![];
            for i in 0..arr.len() {
                res.push(arr[i].as_i64().ok_or("invalid input")? + 1);
                std::thread::sleep(std::time::Duration::from_secs(1));
                self.update_progress(i as f32 / arr.len() as f32, TaskStatus::Running);
                self.log(format!("list_add_1: {} -> {}", i, res[i]));
            }
            Ok(json!(res))
        }
        pub fn int_mul_2(&self, inputs: Value) -> Result<Value, String> {
            let val = inputs.as_i64().ok_or("invalid input")?;
        Ok(json!(val * 2))
    }
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    service(8101).await
}
    
    